clear
set more off
set type double
graph drop _all
program drop _all
set scheme s2mono
import excel "\\file\UsersW$\wrr15\Home\My Documents\My Files\ANDY KIM\REVISION FOR WORLD DEVELOPMENT\DATA AND CODE\MA Data (13 July 2016).xlsx", firstrow case(lower)
log using "\\file\UsersW$\wrr15\Home\My Documents\My Files\ANDY KIM\REVISION FOR WORLD DEVELOPMENT\DATA AND CODE\Part1.smcl", replace


//CONVERTING TSTATS TO PARTICAL CORRELATION COEFFICIENTS, r
gen eff = sqrt(tstat^2/(tstat^2+df))
gen r = eff if positive == 1
replace r = -1*eff if r == .
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r

// RECODING VARIABLES
// The first thing we do is generate some new variables
// because some of our old study characteristics had too few
// observations
gen endog = (tsls == 1 | gmm == 1)
gen se_notols = (se_het == 1 | se_other == 1)
gen fdigdp = fditype0


*--------------------------------------------------------------------------------*
*   TABLE 2: Selected summary statistics                              *
*--------------------------------------------------------------------------------*

summ tstat pcc seR pubyear datayear span initial journalrank

summ pcc if pubyear < 2006
summ pcc if pubyear >= 2006

summ pcc if cjournal == 1
summ pcc if ejournal == 1
summ pcc if notjournal == 1

summ pcc if cs == 1
sum pcc if panel_nofe == 1
summ pcc if panel_fe == 1
summ pcc if ts == 1

summ pcc if ols == 1
summ pcc if gls == 1
summ pcc if tsls == 1
summ pcc if gmm == 1



*--------------------------------------------------------------------------------*
*   FIGURE 1: Histograms of tstat and PCC                                                                  *
*--------------------------------------------------------------------------------*
histogram tstat, bin(20)
graph save histogram(tstat), replace

histogram pcc, bin(20) 
graph save histogram(pcc), replace



*--------------------------------------------------------------------------------*
*   FIGURE 2: Forest Plot by Study                                                                 *
*--------------------------------------------------------------------------------*

/*
//FORREST PLOT BY STUDY (Fixed Effects)
gen fevarR = varR
gen feweightR = 1/fevarR
gen rR = r*feweightR
collapse (sum) rR feweightR, by (id)
gen avgR = rR/feweightR
gen avgsterrR = sqrt(1/feweightR)
sort id
metan avgR avgsterrR, fixed lcols(id) nobox
graph save forrest, replace 
*/

*--------------------------------------------------------------------------------*
*   FIGURE 3A: Funnel Plot for individual estimates                               *
*--------------------------------------------------------------------------------*
//FUNNEL GRAPH
metafunnel r seR, ylabel(#8)  //name(funnel, replace) nolines
graph save funnel1, replace
//NOTE: No evidence of asymmetry in estimates, hence no evidence of publication bias
//NOTE: But much evidence of random effects!

*--------------------------------------------------------------------------------*
*   FIGURE 3B: Funnel Plot for study means                           *
*--------------------------------------------------------------------------------*
//FUNNEL GRAPH
by id, sort: egen meanr = mean(r)
by id, sort: egen meanseR = mean(seR)
metafunnel meanr meanseR, ylabel(#8)  //name(funnel, replace) nolines
graph save funnel2, replace
//NOTE: No evidence of asymmetry in estimates, hence no evidence of publication bias
//NOTE: But much evidence of random effects!

*/

// Generating transformed variables for FE and RE
gen feprecisionR = 1/seR
gen fetstatR = r/seR

*--------------------------------------------------------------------------------*
*   TABLE 3: FAT/PET                                                             *
*--------------------------------------------------------------------------------*

// Fixed Effects with SE
//NOTE: If the coefficient on the constant term is significant, that 
//is evidence of publication bias
// Fixed Effects
//This regression gives equal weight to each observation
// TABLE 3 - Column 1
regress fetstatR feprecisionR, vce(cluster id)
//This regression gives equal weight to each study
// TABLE 3 - Column 2
regress fetstatR feprecisionR [pweight = weight], vce(cluster id)

// Fixed Effects without SE
//NOTE: If the coefficient on the constant term is significant, that 
//is evidence of publication bias
// Fixed Effects
//This regression gives equal weight to each observation
// TABLE 3 - Column 5
regress fetstatR feprecisionR, noc vce(cluster id)
//This regression gives equal weight to each study
// TABLE 3 - Column 6
regress fetstatR feprecisionR [pweight = weight], noc vce(cluster id)


metan r seR, random
scalar tau2 = r(tau2)
gen revarR = varR + tau2
gen reseR = sqrt(revarR)
gen reprecisionR = 1/reseR
gen retstatR = r/reseR

// Random Effects with SE
//This regression gives equal weight to each observation
// TABLE 3 - Column 3
regress retstatR reprecisionR,  vce(cluster id)
//This regression gives equal weight to each study
// TABLE 3 - Column 4
regress retstatR reprecisionR [pweight = weight], vce(cluster id)



log close

